<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="企业号" prop="customerNo">
        <el-input
          v-model="queryParams.customerNo"
          placeholder="请输入企业号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业成立时间" prop="customerCreateTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.customerCreateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择企业成立时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="法定代表人" prop="legalPerson">
        <el-input
          v-model="queryParams.legalPerson"
          placeholder="请输入法定代表人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactMobile">
        <el-input
          v-model="queryParams.contactMobile"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['operater:customer:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operater:customer:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operater:customer:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['operater:customer:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="customerList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业id" align="center" prop="id" />
      <el-table-column label="企业号" align="center" prop="customerNo" />
      <el-table-column label="企业名称" align="center" prop="customerName" />
      <el-table-column
        label="企业成立时间"
        align="center"
        prop="customerCreateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.customerCreateTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="企业注册资本"
        align="center"
        prop="customerRegisteredCapital"
      />
      <el-table-column
        label="营业执照"
        align="center"
        prop="businessLicenseImg"
      >
        <template slot-scope="scope">
          <span @click="lookAtLicenseImg(scope.row.businessLicenseImg)"
            >查看</span
          >
        </template>
      </el-table-column>
      <el-table-column label="法定代表人" align="center" prop="legalPerson" />
      <el-table-column label="社会信用代码" align="center" prop="socialCode" />
      <el-table-column label="开户行" align="center" prop="bankName" />
      <el-table-column label="银行账号" align="center" prop="bankAccount" />
      <el-table-column label="注册地址" align="center" prop="registerAddress" />
      <el-table-column label="办公地址" align="center" prop="office" />
      <el-table-column label="单位电话" align="center" prop="workTelephone" />
      <el-table-column label="邮政编码" align="center" prop="postCode" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactMobile" />
      <el-table-column label="电子邮箱" align="center" prop="email" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['operater:customer:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['operater:customer:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="企业号" prop="customerNo">
          <el-input v-model="form.customerNo" placeholder="请输入企业号" />
        </el-form-item>
        <el-form-item label="企业名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业成立时间" prop="customerCreateTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.customerCreateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择企业成立时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="企业注册资本" prop="customerRegisteredCapital">
          <el-input
            v-model="form.customerRegisteredCapital"
            placeholder="请输入企业注册资本"
          />
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicenseImg">
          <el-upload
            ref="uploadFaceImg"
            class="avatar-uploader"
            :action="uploadFaceImgAction"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="法定代表人" prop="legalPerson">
          <el-input v-model="form.legalPerson" placeholder="请输入法定代表人" />
        </el-form-item>
        <el-form-item label="社会信用代码" prop="socialCode">
          <el-input
            v-model="form.socialCode"
            placeholder="请输入社会信用代码"
          />
        </el-form-item>
        <el-form-item label="开户行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="注册地址" prop="registerAddress">
          <el-input
            v-model="form.registerAddress"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="办公地址" prop="office">
          <el-input
            v-model="form.office"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="单位电话" prop="workTelephone">
          <el-input v-model="form.workTelephone" placeholder="请输入单位电话" />
        </el-form-item>
        <el-form-item label="邮政编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactMobile">
          <el-input v-model="form.contactMobile" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <div class="img-warpper" v-show="showImg" @click="showImg = !showImg">
      <div class="img">
        <img :src="businessLicenseImgUrl" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import {
  listCustomer,
  getCustomer,
  delCustomer,
  addCustomer,
  updateCustomer,
  exportCustomer
} from "@/api/company/customer";

import { uploadFileUrl, baseUrl } from "@/api/config";
export default {
  name: "Customer",
  components: {},
  data() {
    return {
      showImg: false,
      uploadFaceImgAction: uploadFileUrl,
      imageUrl: "",
      businessLicenseImgUrl: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerNo: null,
        customerName: null,
        customerCreateTime: null,
        customerRegisteredCapital: null,
        businessLicenseImg: null,
        legalPerson: null,
        socialCode: null,
        bankName: null,
        bankAccount: null,
        registerAddress: null,
        office: null,
        workTelephone: null,
        postCode: null,
        contactPerson: null,
        contactMobile: null,
        email: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**查看营业执照*/
    lookAtLicenseImg(id) {
      this.businessLicenseImgUrl = baseUrl(id);
      this.showImg = true;
    },
    //上传营业执照
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      // console.log(res);s
      if (res.code === 200) {
        this.imageUrl = res.data.fileUrl;
        this.form.businessLicenseImg = res.data.id;
      } else {
        this.$message.error(res.msg);
      }
    },
    beforeAvatarUpload(file) {
      console.log(file);
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
    /** 查询企业信息列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerNo: null,
        customerName: null,
        customerCreateTime: null,
        customerRegisteredCapital: null,
        businessLicenseImg: null,
        legalPerson: null,
        socialCode: null,
        bankName: null,
        bankAccount: null,
        registerAddress: null,
        office: null,
        workTelephone: null,
        postCode: null,
        contactPerson: null,
        contactMobile: null,
        email: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.imageUrl = baseUrl(row.businessLicenseImg);
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除企业信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delCustomer(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有企业信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportCustomer(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    }
  }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  width: 300px;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 300px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 100%;
  height: 178px;
  display: block;
}
.title-wrapper {
  padding: 24px;
}
.el-upload__tip {
  line-height: 1.2;
}
.img-warpper {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  z-index: 99;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  .img {
    padding: 20px;
    background: #fff;
    max-width: 80%;
    max-height: 40%;
  }
}
</style>
